package cn.mxh.pethspt.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据源连接池工具
 */
public class DruidUtils {
    public static DruidDataSource druidDataSource;// 数据源
    // 一次请求（servlet）,多个service和多个dao共用一个connection
    public static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();// JUC 提供本地线程类  一个线程共享一个变量
    static {
        try {
            // 加载属性集配置文件
            InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("database.properties");
            // 属性集文件对象
            Properties properties = new Properties();
            properties.load(inputStream);
            // 创建数据源对象
            druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据源
     *
     * @return
     */
    public static DruidDataSource getDruidDataSource() {
        return druidDataSource;
    }

    /**
     * 获取连接(自动提交)
     *
     * @return
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = threadLocal.get();
            if(connection == null){
                connection = druidDataSource.getConnection();
                threadLocal.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 获取连接(false 取消自动提交)
     *
     * @return
     */
    public static Connection getConnection(boolean isAuto) {
        Connection connection = null;
        try {
            connection = threadLocal.get();
            if(connection == null){
                connection = druidDataSource.getConnection();
                connection.setAutoCommit(isAuto);
                threadLocal.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭连接 归还连接
     *
     * @param connection
     */
    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
                threadLocal.set(null);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接 归还连接
     *
     *
     */
    public static void close() {
        try {
            Connection connection = threadLocal.get();
            if (connection != null) {
                connection.close();
                threadLocal.set(null);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 事务提交
     */
    public static void commit(){
        try {
            Connection connection = threadLocal.get();
            if (connection != null) connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 事务回滚
     */
    public static void rollback(){
        try {
            Connection connection = threadLocal.get();
            if (connection != null) connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}